<?php include 'inc/teacher_head.php';?>



<div class="layui-container" style="">

    <div class="layui-form-item" style='width:800px;margin:0 auto;padding-top:30px;'>
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 0px;">
            <legend>创建</legend>
        </fieldset>

    <form class="layui-form layui-form-pane" name='form1'>

        <div class="layui-form-item">
            <label class="layui-form-label">
                调查名称
            </label>
            <div class="layui-input-block">
                <input  class="layui-input" lay-verify="required" lay-vertype="tips" name="name"  type="text" placeholder='请输入调查的名称'/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">
                开始题号
            </label>
            <div class="layui-input-inline">
                <input  class="layui-input" lay-verify="required" lay-vertype="tips" name="begin" type="text" value=1 placeholder='请输入开始题号，数字'/>
            </div>
             <label class="layui-form-label">
                结束题号
            </label>
            <div class="layui-input-inline">
                <input  class="layui-input" lay-verify="required" lay-vertype="tips" name="end" type="text" placeholder='请输入结束题号，数字'/>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">
                标准答案
            </label>
            <div class="layui-input-block">
                <input autocomplete="off" class="layui-input" lay-verify="required" lay-vertype="tips" name="answers" type="text" placeholder='请输入标准答案，不区分大小写'/>
            </div>
        </div>



        <div class="layui-form-item" style="text-align: right;">
            <button class="layui-btn" lay-filter="do" lay-submit type="button" style="background:#05C1AE;font-size:12pt;">
                确 定 创 建
            </button>
        </div>

    </form>
    </div>


    <div class="layui-form-item" style='width:800px;margin:0 auto;padding-top:30px;'>
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 0px;">
            <legend> 管理 </legend>
        </fieldset>


        <div class="layui-form-item">
            <!-- 难题调查列表 -->
            <table  class="layui-table" id="hardCounts" lay-filter="hardCounts"></table>
        </div>


    </div>

</div>

    <script id="switch" type="text/html">
        <input type="checkbox" lay-filter="switch" lay-skin="switch" name='switch'
            lay-text="调查中|已关闭" {{d.switch=== '1' ?'checked':null}} value="{{d.id}}">
    </script>

    <script type="text/html" id="tpl1">
        <span lay-event='view' class="layui-btn layui-btn-sm">结果</span>
        <a href='/teacher/viewCount2?countID={{d.id}}' target="_blank">结果2</a>
        <span lay-event='delete' class="layui-btn layui-btn-sm layui-btn-danger" style='margin-left:40px;'>删除</span>
    </script>

<script language="javascript">
    layui.use(['form', 'element', 'table'], function () {
            var form = layui.form
                ,element = layui.element
                , table = layui.table
                , $ = layui.$;
            $("#nav_count").addClass('layui-this'); //设置导航当前选中项的样式





    form.on('submit(do)', function (data) {
         var oldAnswer = data.field.answers;
        //只保留字母的正则表达式
        var newAnswer = oldAnswer.replace(/[^a-zA-Z]/gi,"");
        console.log(newAnswer);
        $.post('/teacher/hardCount', {answers:newAnswer,data:data.field}, function (r) {
            layer.msg(r);
            if( r=='创建成功'){
                //刷新下面的调查表格
                stuTable.reload();
            }
        });
         return false;
        });

    //渲染难题调查表格
    let stuTable=table.render({
        elem: '#hardCounts'
        , id: 'hardCounts'
        , url: 'getHardCounts'
        ,even:true
        , cols: [[

             {field: 'name',  title: '名称'}
            , {templet: '#switch', align: 'center', title: '调查状态',width:120}
            , {toolbar: '#tpl1', align: 'center', title: '常用操作'}
        ]]
        , height: 500

    });

    //分类表工具条事件
    table.on('tool(hardCounts)',function(obj){
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）
        //删除分类按钮
        if (layEvent==='delete'){
            layer.confirm('真的删除该调查吗？', {icon:3}, function (index) {
                $.post('/teacher/deleteCount',{id:data.id},function(res){
                    layer.close(index);
                    layer.msg('删除成功!');
                    obj.del();
                });
            });
        };//删除调查结束

        if(layEvent==='view'){
            var tbody='';
            $.post('/teacher/viewCount',{data:data},function(r){
                if (r==0){
                    layer.msg('本班暂无学生参与');
                }else{
                    var v=JSON.parse(r);
                    // console.log(v['成绩表']);
                    tbody+='<div style="text-align:center;padding:20px;line-height:20pt;">';
                    tbody+='<br>错误率 > 30%：';
                    $.each(v['高错题'],function(n,value){
                        tbody += '<a class="layui-btn layui-btn-xs layui-btn-danger" style="min-width:30px;" title="错误率:'+value+'">' +n +'</a> ';
                    });
                    tbody+='<br><br><hr>';
                    tbody+='<div style="">未参与名单：<br>'+v.not+'</div>';
                    tbody+='<br><hr>';
                    tbody+='<div>成绩单：'+v['成绩表']+'</div>';
                    tbody+='<br><hr>';
                    tbody+='</div>';
                    $('#resultDiv').html(tbody);
                    layer.open({//弹出页面层
                        type: 1
                        , title: '<h3 style="color:#009688">'+data.name+' 难题错误率调查,参与人数：'+v.total+'</h3>'
                        , content: $('#resultDiv')
                        , area: ['100%', '100%']
                    });
                }


            });
        };//查看调查结果结束


    });

    //开关事件
    form.on('switch(switch)', function (obj) {
            var switch1 = this.checked ? '1' : '0';
            $.post('/teacher/countSwitch', {switch: switch1,id:obj.value});
        });

    // 监听ESC键，取消弹出层
    $(document).keydown(function(e){
        if(e.which == 27){
            layer.closeAll();
        }
    });

});
//End
</script>




<br><br><br><br><br>
<?php include 'inc/teacher_foot.php';?>
<div id='resultDiv' style="display: none;padding:30px;padding-top:0;">


</div>